﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class user_Choose : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        String id = ((Button)sender).CommandArgument.ToString();
        int id_user = (int)Session["id"];
        SqlConnection sqlcon;
        SqlCommand sqlcom;

        String strConnection = "Server=.;Database=keshe2;Integrated Security=SSPI";
        SqlConnection con = new SqlConnection(strConnection);
        con.Open();
        float money = 0;
        String sql = "select 价格 from 商品 where 商品编号 ='" + id + "'";
        sqlcon = new SqlConnection(strConnection);
        sqlcon.Open();
        sqlcom = new SqlCommand(sql, sqlcon);
        SqlDataReader Dr = sqlcom.ExecuteReader();
        try
        {
            while (Dr.Read())
            {
                String m = Dr["价格"].ToString();
                money = Convert.ToSingle(m);
            }
        }
        catch (SqlException ex)
        {
            Response.Write(ex.Message.ToString());
        }
        finally
        {
            Dr.Close();
        }
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        string sql1 = "select * from 选购 where 商品编号='" + id + "'and 客户编号 =" + id_user;
        sqlcon = new SqlConnection(strConnection);
        sqlcon.Open();
        sqlcom = new SqlCommand(sql1, sqlcon);
        SqlDataReader Dr1 = sqlcom.ExecuteReader();
        string sqlstr = "";
        try
        {
            if(Dr1.Read())
            {
                int count = Convert.ToInt32(Dr1["数量"].ToString())+1;
                 sqlstr = "update 选购 set 数量=" + count +"where 商品编号 ='" + id+"'";

            }
            else
            {
                sqlstr = "insert into 选购 (商品编号,客户编号,数量,金额) values ('" + id + "','" + id_user + "','1','" + money + "')";
            }
       
            
        }
        catch (SqlException ex)
        {
            Response.Write(ex.Message.ToString());
        }
        finally
        {
            Dr1.Close();
        }
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
         //sqlstr = "insert into 选购 (商品编号,客户编号,数量,金额) values ('" + id + "','" + id_user + "','1','" + money + "')";
           
           

            /*string sqlstr = "update 选购 set 数量=" + count +"where 商品编号 =" + id;
            sqlcon = new SqlConnection(strConnection);
            sqlcom = new SqlCommand(sqlstr, sqlcon);*/
            
        
        sqlcon = new SqlConnection(strConnection);
        sqlcom = new SqlCommand(sqlstr, sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();

    }
}